<template>
  <view class="container">
    <z-paging-swiper>
      <view slot="top">
        <u-navbar title="活动专区" title-size="36" title-color="#333"></u-navbar>
        <view class="tabsView">
          <u-tabs-swiper
            ref="uTabs"
            font-size="32"
            :list="tabsList"
            :current="current"
            @change="tabsChange"
            :is-scroll="false"
            :show-bar="false"
            active-color="#10A28F"
            swiper-width="750rpx"
          >
          </u-tabs-swiper>
        </view>
      </view>
      <swiper
        class="swiperView"
        :current="scurrent"
        @transition="transition"
        @animationfinish="animationfinish"
      >
        <swiper-item v-for="(sitem, sindex) in tabsList" class="swiperItem" :key="sindex">
          <z-paging
            ref="paging"
            v-model="itemList[sindex]"
            @query="queryList"
            :fixed="true"
            :auto="false"
            :empty-view-img="`${getImageUrl('empty.png')} `"
            empty-view-text="暂无数据~"
            :empty-view-img-style="imgStyle"
          >
            <view v-if="sindex == 0">
              <u-gap height="20" bg-color="#F0F1F2"></u-gap>
              <view class="time-banner">
                <view
                  class="time-item3"
                  v-for="(item, index) in barList"
                  :key="index"
                  :class="index == checkedTime ? 'timeCheck' : ''"
                  @click="onTabs(index)"
                >
                  <p class="txt1">{{ item.startTime }}</p>
                  <p v-if="item.status == 1" class="txt2">即将开始</p>
                  <p v-else-if="item.status == 2" class="txt2">抢购中</p>
                  <p v-else-if="item.status == 3" class="txt2">已结束</p>
                  <p v-else class="txt2">已开抢</p>
                </view>
              </view>
              <view
                v-if="seckillStatus == 1"
                style="color: #fa453c; margin-top: 20rpx; text-align: center"
              >
                /距开始
                <u-count-down
                  separator-size="26"
                  separator-color="#FA453C"
                  font-size="26"
                  :show-days="false"
                  color="#FA453C"
                  :timestamp="barList[checkedTime].syTime"
                ></u-count-down>
                /
              </view>
            </view>
            <view v-if="sindex == 2">
              <view class="fanli-list">
                <view class="box">
                  <view class="item" @click="fanliType = 1" :class="fanliType == 1 ? 'checked' : ''"
                    >链动2+1专区</view
                  >
                  <view class="item" @click="fanliType = 2" :class="fanliType == 2 ? 'checked' : ''"
                    >推三/四返本专区</view
                  >
                </view>
              </view>
              <u-image
                v-if="fanliType == 1"
                class="banner"
                :src="`${getImageUrl('21banner.png')} `"
                width="710rpx"
                height="200rpx"
              ></u-image>
              <u-image
                v-if="fanliType == 2"
                class="banner"
                :src="`${getImageUrl('34banner.png')} `"
                width="710rpx"
                height="200rpx"
              ></u-image>
            </view>
            <view class="goodsItemView" :class="sindex == 1 ? 'goodsItemView-group' : ''">
              <u-gap height="20" bg-color="#F0F1F2"></u-gap>
              <view
                class="itemView"
                v-if="sindex == 0"
                v-for="(item, index1) in itemList[sindex]"
                :key="index1"
              >
                <view class="goods-detail dis-flex">
                  <u-image
                    class="goods-avarar"
                    border-radius="10"
                    :src="item.picUrl"
                    width="180rpx"
                    height="180rpx"
                  ></u-image>
                  <view class="goods-con flex-y-between" style="display: flex; flex-flow: column">
                    <view class="tl twoline-hide">{{ item.name }}</view>
                    <view class="jindu" v-if="seckillStatus != 1">
                      <u-line-progress
                        height="20"
                        class="jindu-t"
                        :show-percent="false"
                        active-color="#FA453C"
                        :percent="Math.floor((item.stock / (item.totalStock + item.stock)) * 100)"
                      ></u-line-progress>
                      <text class="ms">仅剩{{ item.stock }}件</text>
                    </view>
                    <view class="" style="font-size: 24rpx; color: #999999"
                      >限量{{ item.stock }}件</view
                    >
                    <view class="bottom dis-flex flex-y-center flex-y-between">
                      <view class="r">
                        <view class="price">¥{{ (item.seckillPrice * 0.01).toFixed(2) }}</view>
                        <view class="line-price">¥{{ (item.price * 0.01).toFixed(2) }}</view>
                      </view>
                      <view
                        class="bt"
                        v-if="seckillStatus == 2"
                        @click="
                          routeTo(
                            '/goods/flash-goods-details/flash-goods-details?id=' +
                              item.activityId +
                              '&skuId=' +
                              item.skuId
                          )
                        "
                        >立即抢购</view
                      >
                      <view class="bt bt-1" v-else-if="seckillStatus == 3">已结束</view>
                      <view class="bt bt-1" v-else>未开始</view>
                    </view>
                  </view>
                </view>
              </view>
              <view
                class="itemView itemView-group"
                v-if="sindex == 1"
                v-for="(item, index1) in itemList[sindex]"
                :key="index1"
              >
                <view class="goods-detail dis-flex" style="margin-bottom: 30rpx">
                  <u-image
                    class="goods-avarar"
                    :src="item.picUrl"
                    border-radius="10"
                    width="220rpx"
                    height="220rpx"
                  ></u-image>
                  <view class="goods-con dis-flex flex-y-between" style="flex-flow: column">
                    <view class="tl twoline-hide">{{ item.name }}</view>
                    <view class="" style="font-size: 26rpx; color: #666666">
                      {{ item.userSize }}人成团
                    </view>
                    <view class="bottom dis-flex flex-y-center flex-y-between">
                      <view class="r">
                        <view class="price" style="margin-bottom: 10rpx"
                          ><text style="font-size: 24rpx; margin-right: 5rpx">拼团价¥ </text
                          >{{ (item.combinationPrice * 0.01).toFixed(2) }}</view
                        >
                        <view class="line-price">¥{{ (item.price * 0.01).toFixed(2) }}</view>
                      </view>
                      <view
                        class="bt"
                        @click="
                          routeTo(
                            '/goods/group-goods-details/group-goods-details?id=' +
                              item.activityId +
                              '&skuId=' +
                              item.skuId
                          )
                        "
                        >立即开团</view
                      >
                    </view>
                  </view>
                </view>
                <u-line color="#C1C0C9"></u-line>
              </view>
              <view
                class="itemView itemView-3"
                v-if="sindex == 2"
                v-for="(item, index1) in itemList[sindex]"
                :key="index1"
                @click="onOrderDetail(item)"
              >
                <view
                  class="fanli-detail dis-flex"
                  @click="routeTo('/goods/fanli-goods-details/goods-details')"
                  style="margin-bottom: 30rpx"
                >
                  <u-image
                    class="goods-avarar"
                    :src="item.avatar"
                    width="345rpx"
                    height="345rpx"
                  ></u-image>
                  <view class="goods-con">
                    <view class="tl twoline-hide">{{ item.title }}</view>
                    <view class="sales">已售{{ item.sales }}件</view>
                    <view class="bottom dis-flex flex-y-center flex-y-between">
                      <view class="r">
                        <view class="price"
                          ><text style="font-size: 26rpx">¥ </text>{{ item.price }}</view
                        >
                        <!-- <view class="line-price">¥{{item.lineprice}}</view> -->
                      </view>
                    </view>
                  </view>
                </view>
              </view>
            </view>
          </z-paging>
        </swiper-item>
      </swiper>
    </z-paging-swiper>
  </view>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      fanliType: 1,
      imgStyle: {
        width: '12rem'
      },
      checkedTime: 0,
      show: false,
      current: 2,
      scurrent: 2,
      resStatus: '',
      seckillStatus: 3,
      tabsList: [
        {
          name: '限时秒杀',
          status: '',
          commentStatus: ''
        },
        {
          name: '超值拼团',
          status: 0,
          commentStatus: ''
        },
        {
          name: '返利专区',
          status: 0,
          commentStatus: ''
        }
      ],
      barList: [],
      itemList: [[], [], []],
      order_id: ''
    }
  },
  onLoad(res) {
    this.current = Number(res.index) ? Number(res.index) : 0
    this.scurrent = Number(res.index) ? Number(res.index) : 0
    let now = new Date()
    this.$u.get('/app-api/promotion/seckill-config/list').then((res) => {
      res.data.forEach((item) => {
        item.percent = Math.cbrt((item.totalStock - item.stock) / item.totalStock) * 100
        let str = item.startTime.match(/\d/g)
        let str2 = item.endTime.match(/\d/g)
        let sn = new Date(
          now.getFullYear(),
          now.getMonth(),
          now.getDate(),
          str[0] + str[1],
          str[2] + str[3],
          str[4] + str[5]
        )
        let sn2 = new Date(
          now.getFullYear(),
          now.getMonth(),
          now.getDate(),
          str2[0] + str2[1],
          str[2] + str2[3],
          str2[4] + str2[5]
        )
        item.startTime = item.startTime.substr(0, 5)
        if (now.getTime() > sn.getTime()) {
          if (now.getTime() > sn2.getTime()) {
            item.status = 3
          } else {
            item.status = 2
          }
        } else {
          item.status = 1
          item.syTime = (sn.getTime() - now.getTime()) / 1000
          console.log('item.syTime', item.syTime)
        }
      })
      // let list =[];
      // res.data.forEach(item =>{
      // 	item.startTime =  item.startTime.substr(0,5)
      // 	list.push(item)
      // })
      this.barList = res.data
      this.queryList(1, 10)
    })
  },

  onShow() {
    let self = this
    // this.queryList()
  },
  methods: {
    tabsChange(index) {
      this.scurrent = index
      this.queryList()
    },
    onTabs(index) {
      this.checkedTime = index
      this.seckillStatus = this.barList[this.checkedTime].status
      this.queryList()
    },
    transition(e) {
      let dx = e.detail.dx
      this.$refs.uTabs.setDx(dx)
    },
    animationfinish(e) {
      this.current = e.detail.current
      this.scurrent = e.detail.current
      this.$refs.uTabs.setFinishCurrent(this.scurrent)
      if (this.itemList[this.current].length == 0) {
        this.queryList()
      }
    },
    queryList(pageNo = 1, pageSize = 10) {
      if (pageNo === 1) {
        this.itemList[this.current] = []
      }
      let url = ''
      let params = {
        pageNo,
        pageSize
      }
      if (this.scurrent == 1) {
        url = '/app-api/promotion/combination-activity/page'
      } else if (this.scurrent == 0) {
        url = '/app-api/promotion/seckill-activity/page'

        params.configId = this.barList[this.checkedTime].id
      }
      this.$u.get(url, params).then((res) => {
        let dynamicPaging = this.scurrent > 0 ? 'paging' + this.scurrent : 'paging'
        this.$refs.paging[this.scurrent].complete(res.data.list)
      })
    },
    routeTo(url) {
      uni.navigateTo({
        url: url
      })
    }
  }
}
</script>

<style lang="scss" scoped>
page {
  height: 100%;
  background: #f0f1f2;
}
.goodsItemView {
  padding: 0 20rpx;
}
.itemView {
  height: 240rpx;
  background: #ffffff;
  border-radius: 10rpx;
  padding: 10rpx 20rpx;
  margin-bottom: 20rpx;
  display: flex;
  flex-flow: column;
  justify-content: space-around;
  .top {
    // margin-bottom: 22rpx;
    .title {
      font-weight: 600;
      font-size: 34rpx;
      color: #333333;
    }
    .price {
      font-weight: 600;
      font-size: 34rpx;
      color: #fa453c;
    }
  }
  .yaoqiu {
    // margin-bottom: 22rpx;
    .bq {
      background: #f0f0f0;
      border-radius: 5rpx;
      padding: 8rpx;
      font-size: 20rpx;
      color: #666666;
      margin-right: 10rpx;
    }
  }
  .company {
    font-size: 28rpx;
    color: #333333;
    // margin-bottom: 22rpx;
    .name {
      margin-right: 40rpx;
    }
  }
  .bottom {
    .user {
      .label {
        margin-left: 10rpx;
        font-size: 28rpx;
      }
    }
    .add {
      font-size: 26rpx;
      color: #999999;
      .detail {
        margin-left: 10rpx;
      }
    }
  }
}
.itemView-1 {
  height: 180rpx;
  .company-detail {
    .com-avarar {
      margin-right: 20rpx;
      border-radius: 10rpx;
    }
    .com-d {
      height: 90rpx;
      display: flex;
      flex-flow: column;
      justify-content: space-between;
      padding: 4rpx 0;
      .com-t {
        font-size: 30rpx;
        font-weight: 600;
      }
      .com-c {
        color: #999999;
        font-size: 24rpx;
        .eq {
          border-right: 1rpx solid #999999;
          padding-right: 10rpx;
          margin-right: 14rpx;
        }
        .eq:last-child {
          border: none;
        }
      }
    }
  }
  .zw {
    margin-left: 110rpx;
    color: #666666;
    font-size: 26rpx;
  }
}
.time-banner {
  display: flex;
  justify-content: space-around;
  background-color: #fff;
  padding: 10rpx 0;
  .time-item {
    width: 20%;
  }

  .time-item2 {
    width: 20%;
  }

  .time-item3 {
    width: 20%;
    color: #999999;
    text-align: center;
    .txt1 {
      font-size: 32rpx;
      font-weight: 600;
    }
    .txt2 {
      font-size: 22rpx;
    }
  }
}
.goods-detail {
  .goods-con {
    flex: 1;
    margin-left: 20rpx;
    .tl {
      font-size: 30rpx;
      color: #333333;
      // margin-bottom: 20rpx;
    }
    .jindu {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .jindu-t {
        width: 70%;
      }
      .ms {
        color: #999999;
        font-size: 22rpx;
      }
    }
    .bottom {
      align-items: flex-end;
      .price {
        font-weight: 500;
        font-size: 32rpx;
        color: #fa453c;
      }
      .line-price {
        color: #999999;
        font-size: 24rpx;
        text-decoration: line-through;
      }
      .bt {
        width: 150rpx;
        height: 56rpx;
        background: #10a28f;
        border-radius: 28rpx;
        font-size: 24rpx;
        color: #ffffff;
        line-height: 56rpx;
        text-align: center;
      }
      .bt-1 {
        background: #ffffff;
        border: 1px solid #e6e6e6;
        color: #999999;
      }
    }
  }
}
.goodsItemView-group {
  padding: 0;
}
.itemView-group {
  margin: 0;
  // border-bottom: 1rpx solid #C1C0C9;
  height: auto;
  padding: 30rpx 20rpx 0 20rpx;
  background-color: #fff;
}
.fanli-list {
  background-color: #fff;
  padding: 20rpx 45rpx;
  width: 100%;
  .box {
    width: 100%;
    display: flex;
    justify-content: space-between;
    height: 80rpx;
    line-height: 80rpx;
    border-radius: 40rpx;
    border: 2rpx solid #10a28f;
    .item {
      width: 50%;
      text-align: center;
      font-size: 32rpx;
    }
    .checked {
      background: #10a28f;
      border-radius: 40rpx;
      color: #ffffff;
    }
  }
}
.banner {
  margin: 20rpx 20rpx 0 20rpx;
}
.itemView-3 {
  width: 345rpx;
  display: inline-block;
  margin-right: 20rpx;
  height: 540rpx;
  padding: 0;

  .fanli-detail {
    flex-flow: column;
    .goods-avarar {
      border-radius: 10rpx;
      background-color: #fff;
    }
    .goods-con {
      padding: 20rpx;
      .tl {
        font-size: 28rpx;
        color: #333333;
        margin-bottom: 6rpx;
      }
      .sales {
        font-size: 24rpx;
        color: #999999;
        margin-bottom: 6rpx;
      }
      .bottom {
        font-size: 32rpx;
        color: #fa453c;
      }
    }
  }
}
.itemView-3:nth-child(odd) {
  margin-right: 0;
}
.timeCheck {
  color: #10a28f !important;
  margin-top: -4rpx !important;
  .txt1 {
    font-size: 36rpx !important;
  }
  .text2 {
    font-size: 26rpx !important;
  }
}
</style>
